<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <Button type="primary" @click="handleCreate"> 新增团队 </Button>
        <ImpExcel @success="excelSuccessFn" dateFormat="YYYY-MM-DD">
          <Button class="m-3" :loading="loading"> 导入Excel </Button>
        </ImpExcel>
        <Button type="primary" @click="initCategoryFn" :loading="loading"> 初始化分类 </Button>
        <Button type="primary" @click="initRuleFn" :loading="loading"> 初始化分数规则 </Button>


      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[

            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label:'分数规则',
              onClick: handleScoreView.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
    <RuleModal @register="rule[0]"></RuleModal>
    <Modal v-model:visible="applyViewShow" title="导入信息有误" @ok="applyViewClose">
      <div>
        <p v-for="(item, index) in msgList">{{ item }} </p>
      </div>
    </Modal>
  </div>
</template>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '@/components/Table';
import {teamList, teamRemove, importTeam,initRule,initCategory} from '@/api/heng/ex';
import { useDrawer } from '@/components/Drawer';

import { onMounted,ref } from 'vue';

import FormModal from './FormModal.vue';
import RuleModal from './RuleModal.vue';
const rule= useDrawer();

import { columns, searchFormSchema } from './role.data';
import { useMessage } from "@/hooks/web/useMessage";
import {Button, Modal} from 'ant-design-vue';
import { useGo } from "@/hooks/web/usePage";
import { aoaToSheetXlsx, ImpExcel } from '@/components/Excel';

const go = useGo();

defineOptions({ name: 'ywDeptList' });
const { createMessage } = useMessage();

// const [register, { openModal, closeModal }] = useModal();
const [register, { openDrawer,closeDrawer }] = useDrawer();

const loading=ref(false);
const msgList = ref([]);
const applyViewShow = ref(false);

const [registerTable, { reload }] = useTable({
  title: '团队列表',
  api: teamList,
  columns,
  // titleHelpMessage:"'本组负责人'可修改本 团队的抽成点数",
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true
  },
  pagination: {
    pageSize: 30
  },
  useSearchForm: true,
  showTableSetting: true,
  bordered: true,
  showIndexColumn: false,
  actionColumn: {
    width: 180,
    title: '操作',
    dataIndex: 'action',
    // slots: { customRender: 'action' },
    fixed: undefined,
  },
});



function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

onMounted(() => {
  // dd.ready(function() {
  //
  // }
})


function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleScoreView(record: Recordable) {
 rule[1].openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function initRuleFn(){
  loading.value = true;
  initRule().then(res=>{
    if(res.code == 200){
      reload();
    }else{
      createMessage.error(res.message);
    }
  }).finally(()=>{
    loading.value = false;
  })
}

function initCategoryFn(){
  loading.value = true;
  initCategory().then(res=>{
    if(res.code == 200){
      reload();

    }else{
      createMessage.error(res.message);
    }
  }).finally(()=>{
    loading.value = false;
  })
}

function handleDelete(record: Recordable) {
  //console.log(record);

  teamRemove({teamId:record.teamId} ).then(res => {
    createMessage.success(res.message);
  })
    .catch((res) => {
      createMessage.error(res.message);
    })
    .finally(() => {
      reload();
    });
}

function handleSuccess() {
  reload();
}

function excelSuccessFn(list) {
  let results = list[0].results;
   console.log(results);
  loading.value = true;
  importTeam({list:results})
    .then((res) => {
      loading.value = false;

      if (res.code == 200) {
        if (res.data && res.data.length > 0) {

          msgList.value = res.data;
          applyViewOpen();
        } else {
          createMessage.success('导入成功!');

        }
      } else {
        loading.value = false;

        createMessage.error(res.message);
      }
    })
    .catch((err) => {
      loading.value = false;
      reload();
    });
}

function applyViewOpen() {
  applyViewShow.value = true;
}

function applyViewClose() {
  applyViewShow.value = false;
}
</script>
